package com.maila.calltaxi_driver.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.maila.calltaxi_driver.R;
import com.maila.calltaxi_driver.adapter.HomeOrderAdapter;
import com.maila.calltaxi_driver.data.Order;
import com.maila.calltaxi_driver.utils.StartActivityUtils;
import com.maila.calltaxi_driver.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    ListView orderlv;
    ImageView menu;
    HomeOrderAdapter homeOrderAdapter;
    List<Order> mdata = new ArrayList<>();
    private long firstTime=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setColorNoTranslucent(this,getResources().getColor(R.color.colorPrimaryDark));
        drawerLayout = (DrawerLayout) findViewById(R.id.activity_na);
        navigationView = (NavigationView) findViewById(R.id.nav);
        menu = (ImageView) findViewById(R.id.main_menu);
        orderlv = (ListView) findViewById(R.id.lv_order);
        View headerView = navigationView.getHeaderView(0);//获取头布局
        View header = LayoutInflater.from(this).inflate(R.layout.orderlv_header, null);
        orderlv.addHeaderView(header);
        ImageView gomall = (ImageView) findViewById(R.id.goto_mall);
        homeOrderAdapter = new HomeOrderAdapter(this);
        initdata();
        homeOrderAdapter.setDatas(mdata);
        orderlv.setAdapter(homeOrderAdapter);
        gomall.setOnClickListener(this);
        menu.setOnClickListener(this);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //item.setChecked(true);
                Toast.makeText(MainActivity.this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                drawerLayout.closeDrawer(navigationView);
                return true;
            }
        });

    }

    private void initdata() {
        Order order1 = new Order();
        order1.setType(1);
        order1.setPositions("潍坊信息职业学院");
        order1.setStatus("现在去接乘客");
        Order order2 = new Order();
        order2.setType(0);
        order2.setPositions("潍坊市高新区人民广场");
        order2.setStatus("明天下午3:50去上车点接乘客");
        mdata.add(order1);
        mdata.add(order2);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {                                         //如果两次按键时间间隔大于2秒，则不退出
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;//更新firstTime
                    return true;
                } else {
//                    MobclickAgent.onKillProcess(this);
                    //两次按键小于2秒时，退出应用
//                    Intent home = new Intent(Intent.ACTION_MAIN);
//                    home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    home.addCategory(Intent.CATEGORY_HOME);
//                    startActivity(home);
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_menu://点击菜单，跳出侧滑菜单
//                if (drawerLayout.isDrawerOpen(navigationView)) {
//                    drawerLayout.closeDrawer(navigationView);
//                } else {
//                    drawerLayout.openDrawer(navigationView);
//                }
                StartActivityUtils.start(this,SettingActivity.class);
                break;
            case R.id.goto_mall:
                StartActivityUtils.start(this, OrderHallActivity.class);
                break;
        }
    }


}
